﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Common;


namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Ajout d'un ou plusieurs fichiers
    /// </summary>
    [Cmdlet(VerbsCommon.Remove, "Files")]
    public class Remove_Files : Cmdlet
    {
        public Remove_Files()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
            }
            this.Question = true;
        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
            }
            try
            {
                // Récupération du Version Control. 
                VersionControlServer versionControl = PsService.VCS(TFS);

                Item[] items = versionControl.GetItems(Source, VersionSpec.Latest, RecursionType.OneLevel).Items;

                string reponse = "y";
                string reponseDeleteFolder = "n";
                if (Question)
                {
                    Console.WriteLine("Are you sure to delete the folder or file ?");
                    reponse = Console.ReadLine();
                    PsLog.AddLog(null, "Are you sure to delete the folder or file ? : Reponse " + reponse);
                }
                if (Question)
                {
                    Console.WriteLine("Do you want to delete the folder too ?");
                    reponseDeleteFolder = Console.ReadLine();
                    PsLog.AddLog(null, "Do you want to delete the folder too  ? : Reponse " + reponseDeleteFolder);
                }

                if (reponse.Equals("y",StringComparison.OrdinalIgnoreCase))
                {
                    foreach (Item item in items)
                    {
                        if (item.ServerItem.ToLower() != Source.ToLower() || reponseDeleteFolder.Equals("y",StringComparison.OrdinalIgnoreCase))
                        {
                            ItemSpec i = new ItemSpec(item.ServerItem, RecursionType.Full);

                            Item[] itemsDestroy = versionControl.Destroy(i, VersionSpec.Latest, null, DestroyFlags);
                            
                            foreach (Item im in itemsDestroy)
                            {
                                PsLog.AddLog(null, "Delete : " + im.ServerItem);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(ex.StackTrace);
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Remove-Files is end");
            base.EndProcessing();
        }

        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        [Parameter(Position = 1, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Source { get; set; }

        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public DestroyFlags DestroyFlags { get; set; }


        [Parameter(Position = 3)]
        [ValidateNotNullOrEmpty]
        public bool Question { get; set; }
    }
}


